package com.s63.appinfo.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtils {

    public static String saveFile(MultipartFile file) throws IOException {
        if (file != null && file.getSize() > 0) {
            String filename = file.getOriginalFilename();
            filename = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
            file.transferTo(new File("C://tmp", filename));
            return filename;
        }
        return null;
    }

    public static void delFile(String filename) {
        if (StringUtils.hasText(filename)) {
            File file = new File("C:/tmp", filename);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
